package com.example.firstspringboot.controller;

import com.example.firstspringboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 使用当前类处理所有与用户相关的业务操作
 */
@Controller
public class UserController {
    private  static File userDir; //    用来表示存放所有用户信息的目录
    static {
        userDir = new File("./users");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }
    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        //注册信息必要性验证
        if(     username == null || username.trim().isEmpty() ||
                password == null || password.trim().isEmpty()||
                nickname == null || nickname.trim().isEmpty()||
                ageStr == null || ageStr.trim().isEmpty()||
                !ageStr.matches("[0-9]+")) {
            //响应错误页面
            try {
                response.sendRedirect("/reg_info_error.html");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return;
        }
        System.out.println(username + "," +password + "," +nickname + "," + ageStr);


        int age = Integer.parseInt(ageStr);
        User user = new User(username,password,nickname,age);
        File file = new File(userDir,username+".obj");
        if(file.exists()){
            try {
                response.sendRedirect("/hava_user.html");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return;
        }
        try (FileOutputStream fos = new FileOutputStream(file);
             ObjectOutputStream oos = new ObjectOutputStream(fos))
        {
            oos.writeObject(user);
            //利用响应对象要求浏览器访问注册成功页面
            response.sendRedirect("/reg_success.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //必要的验证工作
        if(username == null || username.trim().isEmpty()||
           password == null ||password.trim().isEmpty()){
            try {
                response.sendRedirect("login_info_error.html");  //账号密码错误跳转
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return;
        }
        File file = new File(userDir,username+".obj");
        if(file.exists()){//判断用户名是否存在

            try (FileInputStream fis = new FileInputStream(file);
                 ObjectInputStream ois = new ObjectInputStream(fis)){
                User user = (User) ois.readObject(); //读取用户注册信息
                if(user.getPassword().equals(password)){  //判断密码是否一致
                    response.sendRedirect("/login_success.html");
                    return;
                }
            } catch (IOException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            response.sendRedirect("/login_fail.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
